package br.com.JZSolution.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame{
	
	private static final long serialVersionUID = 1L;
	private boolean espera;
	private JPanel blocoLogin;
	
	public MainFrame(){
		//this.setJMenuBar(new Menu());

		//espera = true;
		
		/**Bypass**/
		espera = false;
		carregarTela(new PainelDeAdm());
		/**Bypass**/
		setPreferredSize(new Dimension(1000,600));
		setTitle("Estoque");
		getContentPane().setBackground(Color.BLUE);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		criarTelaLogin();
		pack();
		addKeyListener(new KeyAdapter(){

			@Override
			public void keyPressed(KeyEvent e) {
				abrirTelaLogin();
			}
		});
		addMouseListener(new MouseAdapter(){
			@Override
			public void mousePressed(MouseEvent e) {
				abrirTelaLogin();
			}
		});
		
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				//executar alguma coisa ao fechar
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				System.out.println("Janela fechada");
				//executar ao ter fechado
				/*Usuario user = Estoque.getInstance().getUsuarioAutenticado();
				if(user != null){
					Comunicador comunicador = new Comunicador(Comunicador.LOGOFF, user);
					ControladorDeUsuario.getInstance().comunicarComServidor(comunicador, 1);
					System.exit(0);
				}*/
			}
		});
		
	}	
	
	public void setTelaCheia() throws Exception{
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gd = ge.getDefaultScreenDevice();
		if(gd.isFullScreenSupported()){
			setUndecorated(true);
			gd.setFullScreenWindow(this);
		}	
	}
	
	private void criarTelaLogin(){
		blocoLogin = new JPanel(new GridLayout(3,0));
		PainelDeLogin pl = new PainelDeLogin();
		JPanel painelTemp = new JPanel();
		painelTemp.add(pl);
		painelTemp.setPreferredSize(pl.getPreferredSize());
		
		blocoLogin.add(new JPanel(),0,0);
		blocoLogin.add(painelTemp,1,0);
		blocoLogin.add(new JPanel(),2,0);
	}
	public void abrirTelaLogin(){
		if(isEspera()){
			espera = false;
			getContentPane().add(blocoLogin);
			validate();
			repaint();
		}
	}
	public void fecharTelaLogin(){
		getContentPane().remove(blocoLogin);
		espera = true;
		repaint();
	}
	public void carregarTelaDoCaixa(){
		getContentPane().removeAll();
		getContentPane().add(new PainelDeCaixa());
		validate();
		repaint();
	}
	
	public void carregarTelaDoAdministrador(){
		getContentPane().removeAll();
		getContentPane().add(new PainelDeCaixa());
		validate();
		repaint();
	}
	
	public void carregarTela(JPanel panel){
		getContentPane().removeAll();
		getContentPane().add(panel);
		validate();
		repaint();
	}
	
	public boolean isEspera() {
		return espera;
	}
	
	public JPanel getBlocoLogin() {
		return blocoLogin;
	}
	
}
